namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class IndicatorWilliamsAndRate : BaseIndicator
    {
        private LineShape tlsWR;

        public IndicatorWilliamsAndRate()
        {
            base.IndType = IndicatorType.WR;
            base.IndName = "W&R";
            base.IndDesc = "威廉指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 10.0));
            base.SourceDic["Close"] = "";
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "WR" };
        }

        public override bool Contains(object obj)
        {
            return (obj == this.tlsWR);
        }

        public override double[] Get(int r)
        {
            int num = r - (((int) base.IndPeriod[0].ParamValue) - 1);
            if (num < 0)
            {
                num = 0;
            }
            List<double> valueList = new List<double>();
            List<double> list2 = new List<double>();
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            for (int i = r; i >= num; i--)
            {
                double item = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["High"]]);
                double num5 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["Low"]]);
                valueList.Add(item);
                list2.Add(num5);
            }
            double highValue = Common.GetHighValue(valueList);
            double lowValue = Common.GetLowValue(list2);
            if (highValue != lowValue)
            {
                return new double[] { ((100.0 * (highValue - num2)) / (highValue - lowValue)) };
            }
            return new double[1];
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsWR };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsWR);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double num = this.Get(r)[0];
                        base.Crab.SetValue(this.TlsWR.Name, num, r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.SourceDic["Close"] = source[0];
            base.SourceDic["High"] = source[1];
            base.SourceDic["Low"] = source[2];
            base.IndPeriod[0].ParamValue = periods[0];
            this.TlsWR = base.Crab.AddLine(fields[0], panelID);
        }

        public LineShape TlsWR
        {
            get
            {
                return this.tlsWR;
            }
            set
            {
                this.tlsWR = value;
            }
        }
    }
}

